#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/07/21
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required,current_user
from project.define import manager_required, app_logger
from .page import Messager


RouterName = "records"
# 车辆记录
records = Blueprint(name=RouterName, import_name='project', url_prefix='/records', template_folder='routes/records/html')
PageMessager = Messager()


@records.route('/')
@login_required
def default():
    """
    重定向
    """
    return redirect(url_for('records.parking'))


@records.route('/parking', methods=['POST', 'GET'])
@login_required
def parking():
    """
    在场记录
    """
    view = "parking"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('records_parking.html', **PageMessager.parking_page_data())


@records.route('/entrance', methods=['POST', 'GET'])
@login_required
def entrance():
    """
    入场记录
    """
    view = "entrance"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('records_entrance.html', **PageMessager.entrance_page_data())


@records.route('/exit', methods=['POST', 'GET'])
@login_required
def exit():
    """
    入场记录
    """
    view = "exit"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('records_exit.html', **PageMessager.exit_page_data())


@records.route('/past', methods=['POST', 'GET'])
@login_required
def past():
    """
    通过记录
    """
    view = "past"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：{RouterName}.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    return render_template('records_past.html', **PageMessager.past_page_data())